<?php

class ShareController extends CoreController
{
    public $uid;
    public $role;
    public $role_salt;
    public function init(){
        $this->uid = Yii::app()->user->id;
        $this->role = Yii::app()->user->getState('user_role');
        $this->role_salt = Yii::app()->user->getState('role_salt');
    }
    public function actionComment(){
        if(Yii::app()->request->isAjaxRequest){
            $user_role = $this->getRole($this->role,$this->role_salt,'manage_share_comment');
            if($user_role){
                $share_id = intval(Yii::app()->request->getParam('share_id'));
                $id = intval(Yii::app()->request->getParam('id'));
                $comment_info = Comment::model()->findByPk($id,'share_id=:share_id',array(':share_id'=>$share_id));
                if($comment_info){
                    $uid = intval(Yii::app()->request->getParam('uid'));
                    $user_id = Yii::app()->user->id;
                    $ok = $comment_info->delete();
                    if($ok){
                        Comment::model()->setShareComment($share_id);
                        $data['status'] = 1;
                        $data['message'] = '删除评论成功';
                    }else{
                        $data['status'] = 0;
                        $data['message'] = '删除评论失败';
                    }
                }else{
                    $data['status'] = 0;
                    $data['message'] = '不存在这条评论';
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你没有权限做此操作';
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Xtutuquan::redirect_message('非法操作','error',2);
        }
    }

    public function actionRecommend(){
        if(Yii::app()->request->isAjaxRequest){
            $share_id = intval(Yii::app()->request->getParam('share_id'));
            $sction_code = Yii::app()->request->getParam('sction_code');
            $exceed_time = Yii::app()->request->getParam('exceed_time');
            $tbl_name = Yii::app()->request->getParam('tbl_name');
            if($sction_code==0){
                $data['status'] = 0;
                $data['message'] = '请选择需要推送到的位置';
            }
            if(!$exceed_time){
                //过期时间
                $exceed_time = time() + Yii::app()->params['EXCEED_TIME'];
            }else{
                $exceed_time = strtotime($exceed_time);
            }
            //写入section_recommend表 $section_code,$tbl_name,$tbl_id,$exceed_time
            $ok = SectionRecommend::model()->createData($sction_code,$tbl_name,$share_id,$exceed_time);
            if($ok){
                $data['status'] = 1;
                $data['message'] = '推送成功';
            }else{
                $data['status'] = 0;
                $data['message'] = '推送失败';
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Xtutuquan::redirect_message('非法操作','error',2);
        }
    }

    public function actionMove()
    {
        if(Yii::app()->request->isAjaxRequest){
            $user_role = $this->getRole($this->role,$this->role_salt,'manage_share_move');
            if($user_role){
                $share_id = Yii::app()->request->getParam('share_id');
                $category_id = Yii::app()->request->getParam('category_id');
                $ar_share_info = Share::model()->getSharePk($share_id);
                $ar_share_info->category_id = intval($category_id);
                $ok = $ar_share_info->update();
                if($ok){
                    $data['status'] = 1;
                    $data['message'] = '移动分类成功';
                }else{
                    $data['status'] = 0;
                    $data['message'] = '移动分类失败';
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你没有权限做此操作';
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Xtutuquan::redirect_message('非法操作','error',2);
        }
    }

    public function actionEdit()
    {
        if(Yii::app()->request->isAjaxRequest){
            //查看权限
            $user_role = $this->getRole($this->role,$this->role_salt,'manage_share_edit');
            if($user_role){
                $share_id = Yii::app()->request->getParam('share_id');
                $from = CHtml::encode(Yii::app()->request->getParam('from'));
                $content = CHtml::encode(Yii::app()->request->getParam('content'));
                $ar_share_info = Share::model()->getSharePk($share_id);
                $ar_share_info->from = $from;
                $ok = $ar_share_info->update();
                if($ok){
                    $ar_item_info = Item::model()->getItemPk($ar_share_info->iid);
                    $ar_item_info->content = $content;
                    $ok1 = $ar_item_info->update();
                    if($ok1){
                        $data['status'] = 1;
                        $data['message'] = '修改成功';
                    }else{
                        $data['status'] = 0;
                        $data['message'] = '修改失败';
                    }
                }else{
                    $data['status'] = 0;
                    $data['message'] = '修改失败';
                }
            }else{
                $data['status'] = 0;
                $data['message'] = '你没有权限做此操作';
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Xtutuquan::redirect_message('非法操作','error',2);
        }
    }
	public function actionDelete()
	{
        if(Yii::app()->request->isAjaxRequest){
            //查看权限
            $user_role = $this->getRole($this->role,$this->role_salt,'manage_share_delete');
            if($user_role){
                $id = Yii::app()->request->getParam('id');
                $ar_share_info = Share::model()->getSharePk($id);
                    //删除share
                    $ok = Share::model()->deleteShare($id,$ar_share_info->uid);
                    if($ok){
                        //修改专辑封面
                        Album::model()->updateAlbumCount($ar_share_info->album_id,$ar_share_info->uid);
                        $data['status'] = 1;
                        $data['message'] = '删除图片成功';
                    }else{
                        $data['status'] = 0;
                        $data['message'] = '删除图片失败';
                    }

            }else{
                $data['status'] = 0;
                $data['message'] = '你没有权限做此操作';
            }
            echo json_encode($data);
            Yii::app()->end();
        }else{
            Xtutuquan::redirect_message('非法操作','error',2);
        }
    }
    
}